<?php session_start(); require_once('../connections/con_gl.php'); require './phpmailer/PHPMailerAutoload.php'; ?>
<?php
$act = $_GET[act];
$url = "index.php?component=kontrak";
$ope = $_POST['open'];
// -- validasi
if($act <>'delete') { 
	if (trim($_POST['noreferensi']) == '') {
		$error[] = '- No Kontrak harus diisi !!!';
	}
	// cek kontrak
	mysql_select_db($database_con_gl, $con_gl);
	$query_cekpk = "select no from gl_kontrak where no='$_POST[noreferensi]'";
	$cekpk       = mysql_query($query_cekpk, $con_gl) or die(mysql_error());
	$total_cekpk = mysql_num_rows($cekpk);
	if($total_cekpk > 0 AND $act=='add') {
		$error[] = '- No. Kontak <b>'.$_POST[noreferensi].'</b> sudah digunakan !!!';
	}
	//----
	if ((trim($_POST['tanggal']) == '')) {
		$error[] = '- Tanggal harus diisi !!!';
	}
	if (trim($_POST['dari']) == '') {
		$error[] = '- Customer harus dipilih !!!';
	}
	if ((trim($_POST['awal']) == '') OR (trim($_POST['akhir']) == '')) {
		$error[] = '- Periode Kontrak harus diisi !!!';
	}
	if (trim($_POST['durasi']) == '') {
		$error[] = '- Durasi Kontrak harus dipilih !!!';
	}
	if (trim($_POST['pembayaran']) == '') {
		$error[] = '- Periode Pembayaran harus dipilih !!!';
	}
	/*if ($_POST['ntotal'] <= 0) {
		$error[] = '- Periksa Total Transaksi anda !!!';
	}*/
}
/*End validasi */
if (isset($error)) {
	echo "<img src=\"images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:red;\">Error : </b> <br />".implode("<br />", $error);
}else{
	switch($act) {
	case("delete"):
		$query = "delete from gl_kontrak where no='$_POST[noreferensi]'";
		$query2 = "delete from gl_dkontrak where kontrak='$_POST[noreferensi]'";
		$query3 = "delete from gl_retailtrans where no_ref = '$_POST[noreferensico]'";
		$query4 = "delete from gl_detailtrans where no_ref = '$_POST[noreferensico]'";
		
		// hapus data
		mysql_select_db($database_con_gl, $con_gl);
		
		$runquery = mysql_query($query, $con_gl);
		$runquery = mysql_query($query2, $con_gl);
		$runquery = mysql_query($query3, $con_gl);
		$runquery = mysql_query($query4, $con_gl);
		mysql_query("delete from gl_pembayaran where no_ref = '$_POST[noreferensico]'") or die (mysql_error());			
		if($runquery) { 
			// ---
			mysql_query($query2, $con_gl);
			
			// ---
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil dihapus ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal dihapus !!!";
		}
	break;
	case("edit"):
		mysql_select_db($database_con_gl, $con_gl);
		$pajak = $_SESSION['subtotal']*($_POST['pajak']/100);
		$pph = $_SESSION['subtotal']*($_POST['pph']/100);
		$grandtotal = $_SESSION['subtotal']+$_SESSION['tbiaya']+($_SESSION['subtotal']*$_POST['pajak']/100) - $pph;
		$query="update gl_kontrak set nopo='$_POST[nopo]', kontak='$_POST[dari]', durasi='$_POST[durasi]', pbayar='$_POST[pembayaran]',  tgl='$_POST[tanggal]', mulai='$_POST[awal]', sampai='$_POST[akhir]', info='$_POST[keterangan]', biaya='$_SESSION[tbiaya]', pph='$pph',pajak='$pajak', total='$grandtotal', recipient='$_POST[recipient]' where no='$_POST[noreferensi]'";
		
		//-- save data
		
		// edit retailtrans
		$query_retail = "update gl_retailtrans set tgl='$_POST[awal]',keterangan='$_POST[keterangan]',kontak='$_POST[dari]',jenis='$_POST[jenis]',gudang='$_POST[idgudang]',area='$_POST[idarea]',jatuhtempo='$_POST[akhir]',checkin='$_POST[awal]',checkout='$_POST[akhir]',updated_by = '$_SESSION[nama]', updated_at = '$today',recipient='$_POST[recipient]' where kontrak ='$_POST[noreferensi]'";
		//hapus data pada gl_dkontrak
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		$runqueryretail = mysql_query($query_retail, $con_gl) or die (mysql_error());
		mysql_query("delete from gl_dkontrak where kontrak = '$_POST[noreferensi]'") or die (mysql_error());
		mysql_query("delete from gl_detailtrans where no_ref = '$_POST[noreferensico]'") or die (mysql_error());
	
	
		$i=0;
		
		if(isset($_SESSION['kendaraan'])){
			foreach($_SESSION['kendaraan'] as $kd){
				$harga 	= $_SESSION['hargasatuan'][$i];
				$diskon	= $_SESSION['diskon'][$i];
				$biaya	= $_SESSION['biaya'][$i];
				$diskonamount	= $harga * ($diskon /100);
				
				$total	= $_SESSION['total'][$i];
				$transaksi	= $_SESSION['transaksi'][$i];
				$ppn = $total*($_POST['pajak']/100);
				$pph = $total*($_POST['pph']/100);
				$runquery = mysql_query("insert into gl_dkontrak(kontrak,kendaraan,harga,diskon,biayatambahan,total,transaksi) values ('$_POST[noreferensi]','$kd','$harga','$diskon','$biaya','$total','$transaksi');") or die (mysql_error());
				
				// ini untuk detail trans
				$addquery = mysql_query("INSERT INTO gl_detailtrans (id, no_ref, transaksi, total, barang, kendaraan, ppn,pph, hargasatuan, diskon,biayatambahan,diskonamount) VALUES (NULL, '$_POST[noreferensico]', '$_POST[transaksi]', '$total', '$_POST[barang]', '$kd', '$ppn','$pph','$harga', '$diskon','$biaya','$diskonamount');") or die (mysql_error());
					mysql_query("delete from gl_pembayaran where no_ref = '$_POST[noreferensico]'") or die (mysql_error());			
				$i++;
			}
		}
		
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		if($runquery && $runqueryretail && $addquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
		}
	break;
	default:
		// ---
		list($area) = mysql_fetch_row(mysql_query("select area from gl_gudang where id = '$_POST[gudang]'"));
		$pajak = $_SESSION['subtotal']*($_POST['pajak']/100);
		$pph = $_SESSION['subtotal']*($_POST['pph']/100);
		$grandtotal = $_SESSION['subtotal']+$_SESSION['tbiaya']+($_SESSION['subtotal']*$_POST['pajak']/100) - $pph;
		$query = "insert into gl_kontrak values ('$_POST[noreferensi]','$_POST[nopo]' ,'$_POST[dari]','$_POST[durasi]','$_POST[pembayaran]','$_POST[idarea]','$_POST[idgudang]','$_POST[tanggal]','$_POST[awal]','$_POST[akhir]','$_POST[keterangan]','$_SESSION[tbiaya]','$pajak','$pph','$grandtotal','$_POST[recipient]')";
		// save data kontrak
		
		$nof = $_POST['noreferensico'];
		$encrypt_no_ref = md5($nof);
		
		$query_retail = "insert into gl_retailtrans (no_ref,md5_no_ref,jnsorder,tgl,keterangan,kontak,jenis,gudang,area,jatuhtempo,kontrak,checkin,checkout,recipient,created_by,created_at) values ('$_POST[noreferensico]','$encrypt_no_ref','1','$_POST[awal]','$_POST[keterangan]','$_POST[dari]','$_POST[jenis]','$_POST[idgudang]','$_POST[idarea]','$_POST[akhir]','$_POST[noreferensi]','$_POST[awal]','$_POST[akhir]','$_POST[recipient]','$_SESSION[nama]','$today')";

		
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		$runqueryretail = mysql_query($query_retail, $con_gl) or die (mysql_error());
		
		//Save to gl_retailtrans
		$i=0;
		$kendaraanlist = array();
		if(isset($_SESSION['kendaraan'])){
			foreach($_SESSION['kendaraan'] as $kd){
				$kendaraanlist[$i] = $_SESSION['kendaraan'][$i];
				$harga 	= $_SESSION['hargasatuan'][$i];
				$diskon	= $_SESSION['diskon'][$i];
				$biaya	= $_SESSION['biaya'][$i];
				$diskonamount	= $harga * ($diskon /100);
				
				$total	= $_SESSION['total'][$i];
				$transaksi	= $_SESSION['transaksi'][$i];
				$ppn = $total*($_POST['pajak']/100);
				$pph = $total*($_POST['pph']/100);
				$tgl_mulai_kontrak = $_POST['awal'];
				$tgl_akhir_kontrak = $_POST['akhir'];
				$runquery = mysql_query("insert into gl_dkontrak(kontrak,no_ref,kendaraan,harga,diskon,biayatambahan,total,transaksi,tgl_mulai_kontrak,tgl_akhir_kontrak) values ('$_POST[noreferensi]','$_POST[noreferensico]','$kd','$harga','$diskon','$biaya','$total','$transaksi','$tgl_mulai_kontrak','$tgl_akhir_kontrak');") or die (mysql_error());
				
				// ini untuk detail trans
				$addquery = mysql_query("INSERT INTO gl_detailtrans (id, no_ref, transaksi, total, barang, kendaraan, ppn,pph, hargasatuan, diskon,biayatambahan,diskonamount) VALUES (NULL, '$_POST[noreferensico]', '$_POST[transaksi]', '$total', '$_POST[barang]', '$kd', '$ppn','$pph','$harga', '$diskon','$biaya','$diskonamount');") or die (mysql_error());
				$i++;
			}
		}
		if($runquery && $runqueryretail && $addquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
			mysql_query("delete from gl_kontrak where no = '$_POST[noreferensi]'") or die (mysql_error());
			mysql_query("delete from gl_dkontrak where kontrak = '$_POST[noreferensi]'") or die (mysql_error());
			mysql_query("delete from gl_retailtrans where no_ref = '$_POST[noreferensico]'") or die (mysql_error());
			mysql_query("delete from gl_detailtrans where no_ref = '$_POST[noreferensico]'") or die (mysql_error());
		}
		mysql_select_db($database_con_gl, $con_gl);
 		$customer = mysql_query("SELECT nama,tlp,email from gl_kontak WHERE nama = '$_POST[qtxt]' AND type = 'Customer' ", $con_gl) or die(mysql_error());
		$row_customer = mysql_fetch_assoc($customer);
		
		$kendaraan =  implode(",", $kendaraanlist);
		
		//email konfirmasi
		$mail = new PHPMailer();

		$mail->isSMTP();

		$mail->SMTPAuth = TRUE;
		$mail->SMTPSecure = 'ssl'; //tls or ssl

		//$mail->Host = 'smtp.gmail.com';
		//$mail->Host = 'smtp.googlemail.com';
		$mail->Host = 'mail.cipaganti.co.id';

		//ssl -> 465; tls -> 587
		$mail->Port = 465;

		/**
		 * konfigurasi email pengirim
		 */

		$mail->Username = 'rentcar@cipaganti.co.id';
		$mail->Password = 'r1k1g4nt3n6';
		$mail->From = 'rentcar@cipaganti.co.id';
		/*
		$mail->Username = 'deryfath@gmail.com';
		$mail->Password = 'hiduprefac';
		$mail->From = 'deryfath@gmail.com';
		*/
		$mail->FromName = "RENTCAR CIPAGANTI";

		/**
		 * konfigurasi email penerima
		 */
		 // masis coba email dlu
		$mail->AddAddress($_POST['emailalt'], 'Dery');
		$mail->AddCC('mricky.it@gmail.com','Dede');
		/**
		 * konfigurasi pesan
		 */
		$checkin = date('d F Y', strtotime($_POST['checkin']));
		$checkout = date('d F Y', strtotime($_POST['checkout']));
		
		
		
		$mail->isHTML(true);
		$mail->Subject = 'TICKETING CONFIRMATION';
		
		$url = "http://localhost/cipaganti-rentcar/proses/mailer.php?asd=$encrypt_no_ref";
		//$url = "http://192.168.112.243/rental/proses/mailer.php/";
		$mail->AddEmbeddedImage('images/button.png', 'button', 'images/button.png');
		$myBody = "<html>";
		$myBody .= "<head>";
		$myBody .= "<title>CORPORATE TICKETING CONFIRMATION</title>";
		//$myBody .= "<link href=\"button.css\" rel=\"stylesheet\" type=\"text/css\" />";
		$myBody .= "</head>";
		$myBody .= "<body>";
		$myBody .= "<table width=\"600\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">";
		$myBody .= "<tr>";
		$myBody .= "<td align=\"center\" valign=\"middle\" bgcolor=\"#000000\" style=\"background-color:#000000; padding:20px; color:#ffffff;\">";
		$myBody .= "<div style=\"font-size:24px;\">CIPAGANTI CAR RENTAL</div>";
		$myBody .= "</td>";
		//$myBody .= "<td align=\"right\" valign=\"top\"><img src=\"cid:logoimg\" width=\"197\" height=\"182\" style=\"display:block;\"></td>";
		$myBody .= "</tr>";
		$myBody .= "</table>";
		$myBody .= "<table width=\"600\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#971800\" style=\"background-color:#971800;\">";
		$myBody .=  "<tr>";
		$myBody .= 		"<td align=\"center\" valign=\"middle\" bgcolor=\"#971800\" style=\"background-color:#971800; padding:4px; color:#fc9600;\">";
		$myBody .= 		"<div style=\"font-size:14px;\"><b>CAR BOOKING CONFIRMATION<b></div>";
		$myBody .= 		"</td>";
		$myBody .=	"</tr>";
		$myBody .=	"<tr>";
		$myBody .=	"<td align=\"left\" valign=\"top\" bgcolor=\"#e7e0b7\" style=\"background-color:#e7e0b7; padding:20px;\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\" style=\"margin-bottom:10px;\">";
		$myBody .=		"<div style=\"font-size:14px; color:#000000;\">";
		$myBody .=		"Dear CIPAGANTI, Thank you for choosing Tiket.com as your online ticketing & booking partner! <br><br> This is just to inform you that the following customer with Tiket.com's Order ID #22013019 and Item Number 11784716 has booked a car. <br><br>
						Kindly reserve the booking of our customers and allotment in your TiketConnect module.<br><br>
						Here is your guest reservation information:</div><br>";
		$myBody .=		"<hr align=\"center\"><br>";
		$myBody .=		"<div style=\"font-size:14px;  color:#000000\"> ";
		$myBody .=		"<pre><font face =\"Arial\"><b>Guest Name</b>		: $row_customer[nama] </font></pre>
						<pre><font face =\"Arial\"><b>Phone</b> 			: $row_customer[tlp] </font></pre> 
						<pre><font face =\"Arial\"><b>Car Name</b> 		    :  $kendaraan</font></pre>
						<pre><font face =\"Arial\"><b>Service Type</b>		: Rental Corporate </font></pre>
						<pre><font face =\"Arial\"><b>Start Date</b>			: $_POST[awal] </font></pre>
						<pre><font face =\"Arial\"><b>End Date</b> 			: $_POST[akhir] </font></pre>
						<pre><font face =\"Arial\"><b>Regional Area </b>	: $_POST[namaarea] </font></pre>
						<pre><font face =\"Arial\"><b>Duration</b>	: $_POST[durasi] </font></pre></div><br>";
		
		$myBody .=		"<hr align=\"center\"><br>";
		$myBody .=		"<div style=\"font-size:14px; color:#000000;\">";
		$myBody .=		"Please confirm that you have received this booking by click submit button below..</div><br>";
		$myBody .= 	 	"<div align=\"center\" ><a href=\"$url\"><img src=\"cid:button\" style=\"display:block;\"></a></div>";
		
		$myBody .= 	 	"</td>";
		$myBody .= 	  "</tr>";
		$myBody .= 	  "</table>";
		
		$myBody .= "</body>";
		$myBody .= "</html>";

		$mail->Body = $myBody;
		//$mail->AltBody = $_POST['email_body'];
		//$mail->msgHTML(file_get_contents('mailer.html'), dirname(__FILE__));
		if (!$mail->Send()) {
			$output = json_encode(array("message" => "Mailer Error: " . $mail->ErrorInfo));
			die($output);
			exit;
		}
		
		$output = json_encode(array("message" => "Email telah terkirim.. ".$_POST['txtemail']));
		die($output);
	break;
	}
}
?>